package com.autumn.chat.task;

import com.autumn.chat.config.GlobalParam;
import com.autumn.chat.core.GPTService;
import com.autumn.chat.struct.gpt.PushMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.BlockingQueue;

/**
 * ClassName: GPTBotTask
 * Description:
 * Author: Shen_Yue
 * Date: 2023/3/4 16:54
 * History:
 * <author>          <time>          <version>          <desc>
 * wangqiuyang    2023/3/4 16:54   @Version 1.0        描述
 */
public class GPTMessageConsumeTask extends Thread{

    private static final Logger log = LoggerFactory.getLogger(GPTMessageConsumeTask.class);

    @Override
    public void run() {
        // 拿到队列
        BlockingQueue<PushMessage> queue = GlobalParam.gptPushQueue;

        while (GlobalParam.GPT_PUSH_STARTED) {

            try {

                // 从队列里面拿到消息
                PushMessage push = queue.take();

                // 拿到内容去请求
                String content = push.getContent();
                String chatResponse = GPTService.getGPTMessage(content);

                if (chatResponse == null) {
                    continue;
                }

                // 输出结果
                String uname = push.getUname();
                log.info("###==========gptChat==========\n###" +
                        uname + "向gpt发起了提问： " + content + "\n" +
                        "###gpt的回答：" + chatResponse.replaceAll("\n","") + "\n" +
                        "###==========ChatEnd==========");


            } catch (InterruptedException e) {
                log.error("",e);
            }
        }

        log.info("gpt消息推送任务检测到项目关闭 进程关闭 ================================");
    }
}
